package com.maas.util.mybatis.jpa.littlenb.common;

import com.littlenb.mybatisjpa.annotation.CodeEnum;
import com.littlenb.mybatisjpa.annotation.MappedJdbcType;
import com.littlenb.mybatisjpa.type.CodeType;
import com.littlenb.mybatisjpa.type.IntCodeEnumTypeHandler;
import com.littlenb.mybatisjpa.type.StringCodeEnumTypeHandler;
import java.lang.reflect.Field;
import java.util.Objects;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;

import com.littlenb.mybatisjpa.util.ColumnMetaResolver;
import com.maas.util.mybatis.jpa.littlenb.common.db.JbaDbPluginConst;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.type.BooleanTypeHandler;
import org.apache.ibatis.type.EnumOrdinalTypeHandler;
import org.apache.ibatis.type.EnumTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

/**
 * @author sway.li
 **/
public class MyColumnMetaResolver extends ColumnMetaResolver {
  public static String resolveSqlLike(Field field, String alias) {
    if(JbaDbPluginConst.DIALECT_CURR.equals(JbaDbPluginConst.DIALECT_KINGBASE_V8)||JbaDbPluginConst.DIALECT_CURR.equals(JbaDbPluginConst.DIALECT_ORACLE) ){
      return resolveSqlLikeOracle( field,  alias);
    }else if(JbaDbPluginConst.DIALECT_CURR.equals(JbaDbPluginConst.DIALECT_MYSQL)){
      return resolveSqlLikeMySql( field,  alias);
    }else {
      return resolveSqlLikeOther( field,  alias);
    }
  }
  public static String resolveSqlLikeOther(Field field, String alias) {
    StringBuilder sqlParameter = new StringBuilder();
    //TODO 这样做带来的问题是：会引发sql注入的问题
    sqlParameter.append("'%${");
    if (alias != null && !"".equals(alias)) {
      sqlParameter.append(alias).append(".");
    }
    sqlParameter.append(field.getName());


    sqlParameter.append("}%' ");

    return sqlParameter.toString();
  }


  public static String resolveSqlLikeMySql(Field field, String alias) {
    StringBuilder sqlParameter = new StringBuilder();

    sqlParameter.append("CONCAT('%',#{");
    if (alias != null && !"".equals(alias)) {
      sqlParameter.append(alias).append(".");
    }
    sqlParameter.append(field.getName());

    // jdbcType
    String jdbcType = resolveJdbcAlias(field);

    if (jdbcType != null) {
      sqlParameter.append(", jdbcType=").append(jdbcType);
    }
    // typeHandler
    Class<? extends TypeHandler<?>> typeHandler = resolveTypeHandler(field);

    if (typeHandler != null) {
      sqlParameter.append(", typeHandler=").append(typeHandler.getName());
    }
    sqlParameter.append("},'%') ");

    return sqlParameter.toString();
  }


  public static String resolveSqlLikeOracle(Field field, String alias) {
    StringBuilder sqlParameter = new StringBuilder();

    sqlParameter.append("'%'||#{");
    if (alias != null && !"".equals(alias)) {
      sqlParameter.append(alias).append(".");
    }
    sqlParameter.append(field.getName());

    // jdbcType
    String jdbcType = resolveJdbcAlias(field);

    if (jdbcType != null) {
      sqlParameter.append(", jdbcType=").append(jdbcType);
    }
    // typeHandler
    Class<? extends TypeHandler<?>> typeHandler = resolveTypeHandler(field);

    if (typeHandler != null) {
      sqlParameter.append(", typeHandler=").append(typeHandler.getName());
    }
    sqlParameter.append("}||'%' ");

    return sqlParameter.toString();
  }



}
